<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
  <style>
    .el-page-header__content{
      color: #ffd04b;
    }
  </style>
</head>
<body>
<div id="app">
  <el-page-header  style="color: #fff;
                    line-height: 60px;
                    background-color:#545c64"
                   @back="goBack"
                   content="添加菜品页面">
  </el-page-header>
  <el-card style="width: 600px;height: 700px;margin: 0 auto;background-color: rgba(128,128,128,0.25)">
    <el-form label-width="80px">
      <el-form-item label="创建时间">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" v-model="dish.date1" style="width: 100%;"></el-date-picker>
        </el-col>
        <el-col :span="11">
          <el-time-picker placeholder="选择时间" v-model="dish.date2" style="width: 100%;"></el-time-picker>
        </el-col>
      </el-form-item>
      <el-form-item  label="菜品名称"><el-input v-model="dish.title" type="text"></el-input></el-form-item>
      <el-form-item  label="菜品价格"><el-input v-model="dish.price" type="text"></el-input></el-form-item>

      <el-form-item label="菜品类别">
      <el-select v-model="dish.typeId" placeholder="请选择分类">
        <el-option :label="t.name" :value="t.id" v-for="t in typeArr"></el-option>

      </el-select>
      </el-form-item>
      <el-form-item label="菜品图片">
        <el-upload
                action="/upload"
                list-type="picture-card"
                name="picFile"
                limit="1"
                :on-success="handleSuccess"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="insert()">开始上传</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!--上传组件-->




</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
 let v = new Vue({
    el: '#app',
    data: function() {
      return {
        dialogImageUrl:'',
        dialogVisible: false,
        typeArr:[],
        dish:{title: '',
          price: '',
          date1:"",
          date2:"",
          created:this.date1+this.date2,
          typeId: '',
          url:""
        }
      }
    },
   methods:{
     goBack() {
       history.back();
     },
     handleRemove(file, fileList) {
       v.dish.url="";
       console.log(file, fileList);
       axios.post("/remove?url="+file.response).then(function (response) {

       })
     },
     handlePictureCardPreview(file) {
       this.dialogImageUrl = file.url;
       this.dialogVisible = true;
     },
     handleSuccess(response,file,fileList){
       v.dish.url=response;
       console.log(v.dish.url)
     },
     insert(){
       if(v.dish.url==""){
         v.$message.error("请选择上传的图片！");
         return;
       }
       axios.post("/dish/insert",v.dish).then(function (response) {

         location.href="/admin.html"
       })
     }
   },
   created:function () {
     axios.get("/type/select").then(function (response) {
       v.typeArr = response.data;
     })
   }
  })
</script>
</html>